package dhm.com.hpx.fragment;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import dhm.com.hpx.R;
import dhm.com.hpx.activity.AddspeechcraftActivity;
import dhm.com.hpx.adapter.SpeechcraftAdapter;
import dhm.com.hpx.base.BaseFragment;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.Bean;
import dhm.com.hpx.entity.SpeechcraftListBean;
import dhm.com.hpx.utils.SpUtils;

/**
 * A simple {@link Fragment} subclass.
 */
@SuppressLint("ValidFragment")
public class SpeechcraftFragment extends BaseFragment implements LoginContract.IView {

    @BindView(R.id.recy)
    XRecyclerView recy;
    @BindView(R.id.add)
    TextView add;
    Unbinder unbinder;

    private int cat_id;

    private View rootView;
    private PressenterImpl pressenter;
    private String uid;
    private List<SpeechcraftListBean.DataBean> listBeanData;
    private SpeechcraftAdapter adapter;

    @Override
    protected int getLayout() {
        return R.layout.fragment_speechcraft;
    }

    private int page = 1;

    public SpeechcraftFragment(int cat_id) {
        this.cat_id = cat_id;
    }

    @Override
    protected void initView() {
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        uid = SpUtils.getString(getActivity(), "uid");

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), AddspeechcraftActivity.class);
                intent.putExtra("cat_id", cat_id + "");
                intent.putExtra("type", "3");
                getActivity().startActivity(intent);
            }
        });

        recy.setLayoutManager(manager);

        recy.setLoadingMoreEnabled(true);
        recy.setPullRefreshEnabled(true);

        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getdata();
            }

            @Override
            public void onLoadMore() {
                getdata();
            }
        });

        initData();
    }

    private void getdata() {
        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid", uid);
        map.put("cat_id", cat_id + "");
        map.put("page", page + "");

        pressenter.sendMessage(getActivity(), Constant.speechcraft_list, map, SpeechcraftListBean.class);

    }

    @Override
    protected void initData() {

        isLoad=false;

        page = 1;
        getdata();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_speechcraft, container, false);
        }
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof SpeechcraftListBean) {
            final SpeechcraftListBean speechcraftListBean = (SpeechcraftListBean) data;
            if (speechcraftListBean.getCode() == 1) {
                if (page == 1) {
                    recy.setVisibility(View.VISIBLE);
                    listBeanData = speechcraftListBean.getData();
                    adapter = new SpeechcraftAdapter(getActivity());
                    adapter.setaddClick(new SpeechcraftAdapter.OnIntentClick() {
                        @Override
                        public void item(int posttion) {
                            Map<String, String> map = new HashMap<>();
                            map.put("token", Constant.TOKEN);
                            map.put("uid", uid);
                            map.put("speechcraft_id", listBeanData.get(posttion).getSpeechcraft_id() + "");
                            pressenter.sendMessage(getActivity(), Constant.speechcraftdel, map, Bean.class);
                        }
                    });

                    adapter.setdeleteClick(new SpeechcraftAdapter.OnClick() {
                        @Override
                        public void item(int posttion) {
                            //获取剪贴板管理器：
                            ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
                            ClipData mClipData = ClipData.newPlainText(null, speechcraftListBean.getData().get(posttion).getContent());
// 将ClipData内容放到系统剪贴板里。
                            cm.setPrimaryClip(mClipData);
                            Toast.makeText(getActivity(), "复制成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                    recy.setAdapter(adapter);
                } else {
                    listBeanData.addAll(speechcraftListBean.getData());
                }
                adapter.setList(listBeanData);
                page++;
            }else {
                if (page==1){
                    recy.setVisibility(View.GONE);
                }
            }
            recy.refreshComplete();
            recy.loadMoreComplete();
        } else if (data instanceof Bean) {
            Bean bean = (Bean) data;
            Toast.makeText(getActivity(), bean.getMessage(), Toast.LENGTH_SHORT).show();
            if (bean.getCode() == 1) {
                page = 1;
                getdata();
            }
        }
    }

    @Override
    public void fail(String error) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
